package com.clouddot.baipai.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.clouddot.baipai.R;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.infrastructure.environment.Environment;
import com.clouddot.baipai.infrastructure.environment.initial.InitialAppUpdate;
import com.clouddot.baipai.ui.dialog.A_updateDailog;
import com.clouddot.baipai.ui.dialog.V_Dialog.V_UpdateDailog;
import com.clouddot.baipai.ui.util.Ftp.DownFileUtil;
import com.clouddot.baipai.ui.util.SlideSwitch;
import com.clouddot.baipai.ui.util.ToastUtil;

/**
 * Created by wxy on 2018/4/10.
 */

public class A_Set2Fragment extends BaseFragment{

    private View view;
    private Activity activity;

    private RelativeLayout headUpdate;
    private RelativeLayout autoContent;
    private SlideSwitch isAuto,isTakePhoto,isOpenService;
    private RadioButton one,two,three,four,five;
    private RadioGroup radioGroup;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


        return view;
    }

    @Override
    protected void setContentView() {
        super.setContentView();
        if(Environment.getInstance().Height <= 100){
            view = LayoutInflater.from(activity).inflate(R.layout.h_f_set2fg, null);
        }else {
            view = LayoutInflater.from(activity).inflate(R.layout.v_f_set2fg, null);

        }
    }

    @Override
    protected void initControls() {
        super.initControls();
        headUpdate = view.findViewById(R.id.headUpdate);
        autoContent = view.findViewById(R.id.autoContent);
        isAuto = view.findViewById(R.id.isAuto);
        isOpenService = view.findViewById(R.id.isOpenService);
        one = view.findViewById(R.id.thrity_mini);
        two = view.findViewById(R.id.one_hour);
        three = view.findViewById(R.id.two_hour);
        four = view.findViewById(R.id.three_hour);
        five = view.findViewById(R.id.four_hour);

        radioGroup = view.findViewById(R.id.radio);

        try
        {
            setSwitchCheckedState();
        } catch (Exception e)
        {
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));

        }


    }

    @Override
    protected void setListener() {
        super.setListener();
        headUpdate.setOnClickListener(headUpdateListener);
        isAuto.setSlideListener(isAutolistener);
        radioGroup.setOnCheckedChangeListener(radioGroupListener);
        isOpenService.setSlideListener(isOpenServicelistener);
    }


    @Override
    protected void loadParams() {
        super.loadParams();
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        this.activity = activity;

    }


    private SlideSwitch.SlideListener isAutolistener = new SlideSwitch.SlideListener() {
        @Override
        public void open() throws Exception {
            autoContent.setVisibility(View.VISIBLE);
            int time = (int) Env.getHelper().get("time",02);
            if(time == 00){
                one.setChecked(true);
            }else if(time == 01){
                two.setChecked(true);
            }else if(time == 02){
                three.setChecked(true);
            }else if(time == 03){
                four.setChecked(true);
            }else if(time == 04){
                five.setChecked(true);
            }
            Env.getHelper().put("isAuto" ,true);
            Env.getAppContext().isAutoUpdate = true;

        }
        @Override
        public void close() throws Exception {
            autoContent.setVisibility(View.GONE);
            Env.getHelper().put("isAuto" ,false);
            Env.getAppContext().isAutoUpdate = false;

        }
    };

    private SlideSwitch.SlideListener isAutoTakePhotolistener = new SlideSwitch.SlideListener() {
        @Override
        public void open() throws Exception {
            Env.getHelper().put("isAutoTakePhoto" ,true);
            Env.getAppContext().isAutoTakePhoto = true;

        }
        @Override
        public void close() throws Exception {
            Env.getHelper().put("isAutoTakePhoto" ,false);
            Env.getAppContext().isAutoTakePhoto = false;

        }
    };

    private SlideSwitch.SlideListener isOpenServicelistener = new SlideSwitch.SlideListener() {
        @Override
        public void open() throws Exception {
            Env.getHelper().put("isOpenService" ,true);
            Env.getAppContext().isOpenService = true;

        }
        @Override
        public void close() throws Exception {
            Env.getHelper().put("isOpenService" ,false);
            Env.getAppContext().isOpenService = false;

        }
    };

    private RadioGroup.OnCheckedChangeListener radioGroupListener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int i) {
            RadioButton radioButton = view.findViewById(radioGroup.getCheckedRadioButtonId());
            String string = radioButton.getText().toString();
            if (string.equals("00:00")) {
                Env.getHelper().put("time",00);
            } else if(string.equals("01:00")) {
                Env.getHelper().put("time",01);
            }else if(string.equals("02:00")) {
                Env.getHelper().put("time",02);
            }else if(string.equals("03:00")) {
                Env.getHelper().put("time",03);
            }else if(string.equals("04:00")) {
                Env.getHelper().put("time",04);
            }
            Env.getAppContext().autoUpdateTime = (int) Env.getHelper().get("time",02);
        }
    };

    private View.OnClickListener headUpdateListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startUpdate();
        }
    };

    private void setSwitchCheckedState() throws Exception {
        if (Env.getAppContext().isAutoUpdate) {
            autoContent.setVisibility(View.VISIBLE);
            isAuto.setState(true);
            int time = (int) Env.getHelper().get("time",02);
            if(time == 00){
                one.setChecked(true);
            }else if(time == 01){
                two.setChecked(true);
            }else if(time == 02){
                three.setChecked(true);
            }else if(time == 03){
                four.setChecked(true);
            }else if(time == 04){
                five.setChecked(true);
            }
            Env.getAppContext().autoUpdateTime = time;
        } else {
            autoContent.setVisibility(View.GONE);
            isAuto.setState(false);

        }

        if(Environment.getInstance().Height <= 100) {
            isTakePhoto = view.findViewById(R.id.isAutoTakePhoto);
            isTakePhoto.setSlideListener(isAutoTakePhotolistener);

            if (Env.getAppContext().isAutoTakePhoto) {
                isTakePhoto.setState(true);
            } else {
                isTakePhoto.setState(false);
            }
        }

        if (Env.getAppContext().isOpenService){
            isOpenService.setState(true);
        }else{
            isOpenService.setState(false);
        }

    }
    private LinearLayout[] btns = new LinearLayout[2];
    private Handler handler = new Handler();

    private void startUpdate(){
        try {
            new InitialAppUpdate(Environment.getInstance()).reinitial();
        } catch (Exception e) {
            Env.error(Environment.getInstance().fileUtil.Errorinfo(e));
            e.printStackTrace();
        }
        ToastUtil.showToast(activity,"正在检查版本...");

        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if(Env.getAppContext().isUpdata){
                    if(Environment.getInstance().Height <= 100) {
                        H_update();
                    }else {
                        V_update();
                    }
                }else {
                    ToastUtil.showToast(activity,"当前版本是最新版本");
                }
            }
        },1000);
    }

    private TextView textView;
    private void H_update(){
        final A_updateDailog dailog  = new A_updateDailog(activity);

        btns = dailog.getBtnView();
        textView = dailog.getTextView();

        textView.setText(Env.getAppContext().updateTip);

        dailog.showDialog();

        btns[1].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dailog.closeDialog();
            }
        });

        btns[0].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DownFileUtil.downloadFile(activity);
                dailog.closeDialog();
            }
        });


    }

    private void V_update() {
        final V_UpdateDailog dailog = new V_UpdateDailog(activity);

        btns = dailog.getBtnView();
        textView = dailog.getTextView();

        textView.setText(Env.getAppContext().updateTip);


        dailog.showDialog();

        btns[1].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dailog.closeDialog();
            }
        });

        btns[0].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DownFileUtil.downloadFile(activity);
                dailog.closeDialog();
            }
        });

    }

}
